#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from sqlalchemy import Column, String, Integer, BigInteger, Boolean, DateTime, Text
from sqlalchemy import create_engine, MetaData, Table, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base

# 创建引擎
engine = create_engine("mysql+pymysql://sam:lee@127.0.0.1:3306/wicats", encoding='utf-8', max_overflow=5, pool_size=20, echo=True)
# 创建Model对象的基类
Base = declarative_base()

# 用户角色交叉表
user_role = Table('user_role', Base.metadata,
Column('user_id', BigInteger, primary_key=True),
Column('role_id', BigInteger, primary_key=True))

# 定义User对象
class User(Base):
    # 表的名字
    __tablename__ = 'user'

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    username = Column(String(32), unique=True, nullable=False)
    password = Column(String(128))
    email = Column(String(64), unique=True)
    mobile = Column(String(32), unique=True)
    disabled = Column(Boolean, default=True)
    last_modified = Column(DateTime)
    roles = relationship(
        'Role',
        secondary=user_role,
        backref="users"
    )

# 定义Role对象
class Role(Base):
    __tablename__ = 'role'

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    name = Column(String(32), unique=True, nullable=False)

# 定义Profile对象
class Profile(Base):
    __tablename__ = 'profile'

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    name = Column(String(32))
    avatar = Column(String(255))
    preferences = Column(Text)
    user_id = Column(BigInteger, nullable=False) #对应的用户ID



Base.metadata.create_all(engine)
